package com.common.android.utils.smiley;

import java.util.List;

import com.common.android.utils.db.CommonDatabase;
import com.common.android.utils.db.Query;
import com.common.android.utils.db.RowMapper;
import com.common.android.utils.db.SqliteTemplate;
import com.product.android.business.ApplicationVariable;
import com.product.android.utils.LogUtils;

import android.content.ContentValues;
import android.database.Cursor;

public class SmileyHistoryDao {
	public static final int DELETE_FAIL=-1;
	public static final int EXIST=-2;
	private SqliteTemplate sqliteTemplate;
	public SmileyHistoryDao() {
		sqliteTemplate = new SqliteTemplate(CommonDatabase.INSTANCE.getDb());
	}
	
	public List<Smiley> getSmileyList() {
		Query query = new Query();
		query.from(SmileyHistoryTable.TABLE_NAME, null);
		query.orderBy(SmileyHistoryTable.FIELD_TIME + " DESC");
		List<Smiley> list = sqliteTemplate.queryForList(query,new SmileyMapper());
		return list;
	}

	
	/**
	 * @n<b>函数名称</b>     :insertSmiley
	 * @brief 把表情插入到数据库，由于这个操作可能多线程并发，且其中还有删除操作，需要同步保护
	 * @version    Ver 1.3.3_modularization
	 * @param  @param smiley
	 * @param  @param max
	 * @param  @return
	 * @return long
	 * @<b>作者</b>          :  cb
	 * @<b>创建时间</b>      :  2014-3-18上午10:38:05      
	*/
	public synchronized long insertSmiley(Smiley smiley,int max) {
		if (smiley==null){
			LogUtils.e(LogUtils.DB_ERROR,"insertSmiley null smiley");
			return -1;
		}
		List<Smiley> list=getSmileyList();
		int count;
		if (list==null){
			count=0;
		}else{
			count=list.size();
		}
		boolean isExist=false;
		for (Smiley exist:list){
			if (exist.id==smiley.id){
				isExist=true;
				if (!deleteSmiley(exist)){
					return DELETE_FAIL;	
				}
			}
		}
		ContentValues values=SmileyToValues(smiley);
		Query query = new Query();
		query.into(SmileyHistoryTable.TABLE_NAME).values(values);
		if (!isExist){
			if (count>=max){
				//数量超过上限，先删除最后一条再插入
				if (!deleteSmiley(list.get(count-1))){
					return DELETE_FAIL;
				}
			}	
		}
		return sqliteTemplate.insert(query);
	}
	
	
	public boolean deleteSmiley(Smiley smiley) {
		if (smiley==null){
			LogUtils.e(LogUtils.DB_ERROR,"deleteSmiley null smiley");
			return false;
		}
		Query query = new Query();
		query.from(SmileyHistoryTable.TABLE_NAME);
		query.where(SmileyHistoryTable.FIELD_SMILEY_ID+ " = ?",smiley.id);
		return sqliteTemplate.delete(query);
	}
	
	private static final class SmileyMapper implements RowMapper<Smiley> {
		
		public Smiley mapRow(Cursor cursor, int rowNum) {
			if (null == cursor || 0 == cursor.getCount()) {
				return null;
			}
			return SmileyHistoryTable.parseCursor(cursor);
		}
	}

	private ContentValues SmileyToValues(Smiley smiley) {
		ContentValues v = new ContentValues();
		//用户根据OAPUID分库后不再需要此字段，为避免升级数据库，仍然往这个字段里写数据
		v.put(SmileyHistoryTable.FIELD_UID, ApplicationVariable.INSTANCE.getIUser().getOapUid());
		
		v.put(SmileyHistoryTable.FIELD_NAME, smiley.name);
		v.put(SmileyHistoryTable.FIELD_PATH, smiley.path);
		v.put(SmileyHistoryTable.FIELD_SHORTCUT, smiley.shortCut);
		v.put(SmileyHistoryTable.FIELD_SMILEY_ID, smiley.id);
		v.put(SmileyHistoryTable.FIELD_TIP, smiley.tip);
		v.put(SmileyHistoryTable.FIELD_TIME, smiley.lastUsedTime);
		return v;
	}
}